﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using Commerce.Common;


namespace CommerceForUmbraco
{

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    public class CommerceService : System.Web.Services.WebService
    {


        [WebMethod(EnableSession = true)]
        public bool AddToCart(int productID, int quantity)
        {
            try
            {
                Commerce.Common.Product prod = new Commerce.Common.Product(productID);
                prod.Quantity = quantity;
                OrderController.AddItem(prod);
                return true;
            }
            catch
            {
                return false;
            }
        }

        [WebMethod(EnableSession = true)]
        public bool RemoveFromCart(int productId)
        {
            try
            {
                OrderController.RemoveItem(productId);
                return true;
            }
            catch
            {
                return false;
            }

        }

        [WebMethod(EnableSession = true)]
        public bool AdjustQuantity(int productId, int newQuantity)
        {
            try
            {
                int orderID = OrderController.GetCartOrderID();
                OrderController.AdjustQuantity(orderID, productId, "", newQuantity);

                return true;
            }
            catch
            {
                return false;
            }
           
        }

        [WebMethod(EnableSession = true)]
        public string GetCheckoutUrl()
        {
            try
            {
                return Utility.GetCheckoutUrl();
            }
            catch
            {
                return string.Empty;
            }
        }

        [WebMethod(EnableSession = true)]
        public string GetCartUrl()
        {
            try
            {
                return Utility.GetCartUrl();
            }
            catch
            {
                return string.Empty;

            }
        }
    }
}
